<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of container
 *
 * @author Moon
 */
class container extends CWidget {
    //put your code here

    /*
     * This ID of the container
     *
     * var string
     */
    public $id = '';

    /*
     * The jquery theme to use for this widgets.
     * By setting value to this array, it will assume that no other extension or code in the application
     * have register coreScript files and the jquery UI css.
     *
     * var string
     */
    public $theme = NULL;
    /*
     * The folder where the theme is stored
     * it will be ignored if there is no value set to $theme
     *
     * var string
     */
    public $themefolder = NULL;

    /*
     * The theme file name
     *
     * var string
     */
    public $themefile = 'jquery-ui.css';

    /*
     * The items that should be load to the container by default
     *
     * var array
     */
    public $items = array();

    public function run() {
        $contents = $this->LoadItems($this->items);
        $html = "<div id='{$this->id}' class='ui-widget ui-widget-content ui-corner-all container'>{$contents}</div>";
        $this->publishAssets();
        echo $html;
    }
    public function publishAssets() {
        $cs = Yii::app()->getClientScript();
        $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'scriptFile';
        $baseURL = Yii::app()->getAssetManager()->publish($dir);
        $cs->registerScriptFile($baseURL.'/js/container.js');
        $cs->registerCssFile($baseURL.'/css/container.css');
        if($this->theme !== NULL) {
            $cs->registerCoreScript('jquery');
            $themecollection = $this->themefolder == null ? $baseURL.'/css/theme':$this->themefolder;
            $cs->registerCssFile($themecollection.'/'.$this->theme.'/'.$this->themefile);
        }
    }
    private function LoadItems($items=array()) {
        if(count($items)<=0)
            return '';
        $item = '';
        foreach($items as $id=>$value) {
            $item .= '<div id="'.$id.'" class="ui-state-highlight ui-corner-all item"><input id="'.$id.'_items" name="'.$this->id.'_items[]" type="hidden" value="'.$id.'" /><a href="#" class="ui-state-default control" onClick="$(this).parent(\'div\').remove()"><span class="ui-icon ui-icon-circle-close"></span></a>'.$value.'</div>';
        }
        return $item;
    }
}
?>
